﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace LambdaProj
{
    public class CommandLineHelper
    {
        public static void Parse(string[] ars,object commandLine)
        {
            string errorMessage;
            
        }

        public static bool TryParse(string[] args,object commandLine,out string errorMessage)
        {
            bool success = false;
            errorMessage = null;
            foreach(string arg in args)
            {
                string option;
                if(arg[0]=='/'||arg[0]=='-')
                {
                    string[] optionParts = arg.Split(new char[]{':'},2);
                    option = optionParts[0].Remove(0,1);
                    PropertyInfo property = commandLine.GetType().GetProperty(option,BindingFlags.IgnoreCase|BindingFlags.Instance|BindingFlags.Public);
                    if(property!=null)
                    {
                        if(property.PropertyType == typeof(bool))
                        {
                            property.SetValue(commandLine,true,null);
                            success = true;
                        }
                        else if(property.PropertyType == typeof(string))
                        {
                            property.SetValue(commandLine,optionParts[1],null);
                            success = true;
                        }
                        else if(property.PropertyType.IsEnum)
                        {
                            try
                            {
                                property.SetValue(commandLine,Enum.Parse(typeof(ProcessPriorityClass),optionParts[1],true),null);
                                success = true;
                            }
                            catch(ArgumentException)
                            {
                                success = false;
                                errorMessage = string.Format("the option '{0} is invalid for '{1}''",optionParts[1],option);
                            }
                        }
                        else
                        {

                        }
                    }

                    
                }
            }

            return success;
        }
    }
}
